package com.slf.flow.controller;

import java.util.Map;

import com.slf.common.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.slf.flow.core.model.FlowNotice;
import com.slf.flow.core.service.FlowNoticeService;
import com.slf.flow.enums.FlowNoticeWay;
import com.slf.flow.model.JsonBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/flowNotice")
public class FlowNoticeController {

	private static final Logger logger = LoggerFactory.getLogger(FlowNoticeController.class);
	@Autowired
	private FlowNoticeService flowNoticeService;

	@PutMapping("/save")
	public Result<FlowNotice> save(FlowNotice entity) {
		Result<FlowNotice> result = new Result<>();
		if (StringUtils.isEmpty(entity.getId())) {
			return result.error("流程id不能为空");
		}
		try {
			flowNoticeService.saveOrUpdate(entity);
		} catch (RuntimeException e) {
			logger.error(e.getMessage(), e);
			return result.error( e.getMessage() == null ? "发生错误" : e.getMessage());
		}
		return result.ok(entity);
	}

	@GetMapping("/get/{id}")
	public Result<FlowNotice> find(@PathVariable("id") String id) {
		FlowNotice data = flowNoticeService.getById(id);
		if (data == null) {
			data = new FlowNotice();
			data.setId(id);
		}
		return new Result<FlowNotice>().ok(data);
	}

	@RequestMapping("/getNoticeWay")
	public Result<Map<String, String>> getNoticeWay(String id) {
		return new Result<Map<String, String>>().ok(FlowNoticeWay.getMap());
	}

}
